#include "rf_bit_proc.h"
#include "hitag.h"
#include "string.h"
#include "uart_hw.h"
#include "debug_prt.h"

/*表1，变量二进制形式1的个数*/
uint8_t  BitMap1[16]={
0 ,1 ,1 ,2 ,1 ,2 ,2 ,3 ,
1 ,2 ,2 ,3 ,2 ,3 ,3 ,4
};


RF_BSP_TypeDef		tagRF_BSP = {0};


uint8_t PIN_RF_SYNC = 0;
uint8_t PIN_First_Valid = 0;
uint8_t PIN_Process_Enable = 0;
uint8_t   Ciphertext_Back_value = 0;
uint8_t   Ciphertext_Count      = 0;
uint8_t   Ciphertext_Buffer[13];

uint8_t   car_key[12];						

#if debug_433_def
unsigned int bitDataRxIndx = 0;
unsigned int bitDataTxIndx = 0;

unsigned char bitShiftCnt = 0;
unsigned char bitDataRx = 0;
unsigned char bitDataTx[defBufLen];
#endif



void detect_key_result(void)
{
	
	#if debug_433_def
      while (bitDataRxIndx != bitDataTxIndx)
      {
        writeUart0OneByte(bitDataTx[bitDataTxIndx]);

				bitDataTx[bitDataTxIndx] = 0;
				
				bitDataTxIndx ++;
				if (bitDataTxIndx >= defBufLen)
				{
				 bitDataTxIndx = 0; 
				}
      }
	#endif  
			
			
	#if 1
		//直接把上层解析放在这里
		if (PIN_Process_Enable == 1)
		{
			if (Hitag2(Ciphertext_Buffer+1) == 1)
			{
				memcpy(car_key, Ciphertext_Buffer+1, 12);
			}
			
			//接收密文处理完毕
			PIN_Process_Enable  = 0;
			PIN_RF_SYNC         = 0;
			
			#if _En_Debug_Prt	//串口发给PC，查看钥匙码信息
				{
					uint32_t i;
					printf("\r\ncar_key:\r\n");
					for (i = 0; i < 12;i++)
					{
						printf("%02x ", car_key[i]);
					}
					printf("\r\n");
				}
			#endif
			
		
		}
		#endif
}


/********************************************
* Function Name: RF_Recevie_Code
* Description  : 接收数据
* Input        : NONE
* Output       : NONE
* Return Value : 返回1代表已解析ok
********************************************/
uint8_t RF_Recevie_Code(uint8_t curbit)
{
	//uint8_t	tmpDa = 0;
	
  uint16_t Cur_Value = 0;

  //读取接收信号
  tagRF_BSP.value<<=1;


	if (curbit)
	{
		SET_BIT(tagRF_BSP.value,0);
			if(tagRF_BSP.Cur_High_bits < 255)
			{
					tagRF_BSP.Cur_High_bits ++;
			}
	}
	else
	{
			CLR_BIT(tagRF_BSP.value,0);
			if(tagRF_BSP.Cur_Low_bits < 255)
			{
					tagRF_BSP.Cur_Low_bits ++;
			}
	}
		
		#if 0//debug_433_def
         if(0 == PIN_Process_Enable)
         {
						bitDataRx <<= 1;
						bitDataRx &= 0xFE;
	
           if (curbit)
           {
              bitDataRx |= 0x01;
           }
           
            bitShiftCnt++;
            if (bitShiftCnt > 7)
            {
              bitDataTx[bitDataRxIndx++] = bitDataRx;
              bitShiftCnt = 0;
              bitDataRx = 0;
              
              if (bitDataRxIndx >= defBufLen)
              {
                bitDataRxIndx = 0;
              }
              
            } 
         }
     #endif
				 

    Cur_Value = BitMap1[tagRF_BSP.value&0x0F] + BitMap1[tagRF_BSP.value>>4];

    if(0 == PIN_RF_SYNC)
    {
        if(2 < Cur_Value)
        {
            //当前状态为高电平
            tagRF_BSP.Cur_state = 1;
        }
        else
        {
            //当前状态为低电平
            tagRF_BSP.Cur_state = 0;
            if((25 < tagRF_BSP.Back_High_bits)
            &&(tagRF_BSP.Back_High_bits < 50))
            {
                if((70 < tagRF_BSP.Cur_Low_bits))
                {
                    //收到同步码
                    tagRF_BSP.Cur_state      = 1;
								    tagRF_BSP.Back_state     = 1;

								    Ciphertext_Count       = 0;
								    Ciphertext_Back_value  = 0;

								    PIN_RF_SYNC            = 1;
								    PIN_First_Valid        = 0;
								    PIN_Process_Enable     = 0;
									
									
									 #if 0//debug_433_def
									if(0 == PIN_Process_Enable)
									{
										tmpDa = 0xAA;
										bitDataTx[bitDataRxIndx++] = tmpDa;
										if (bitDataRxIndx >= defBufLen)
										{
											bitDataRxIndx = 0;
										}
										tmpDa = 0xBB;
										bitDataTx[bitDataRxIndx++] = tmpDa;
										if (bitDataRxIndx >= defBufLen)
										{
											bitDataRxIndx = 0;
										}
									}
										#endif
                }
            }
        }
    }
    else
    {
         if(5 < Cur_Value)
         {
             //当前状态为高电平
             tagRF_BSP.Cur_state = 1;
         }
         else
         {
             //当前状态为低电平
             tagRF_BSP.Cur_state = 0;
         }
         
         #if debug_433_def
         if(0 == PIN_Process_Enable)
         {
           bitDataRx <<= 1;
           if (curbit)
           {
              bitDataRx |= 0x01;
           }
           
            bitShiftCnt++;
            if (bitShiftCnt > 7)
            {
              bitDataTx[bitDataRxIndx++] = bitDataRx;
              bitShiftCnt = 0;
              bitDataRx = 0;
              
              if (bitDataRxIndx >= defBufLen)
              {
                bitDataRxIndx = 0;
              }
              
            } 
         }
          #endif
      
    }


    if(tagRF_BSP.Back_state  != tagRF_BSP.Cur_state)
    {
       
        //电平有变化
        if(1 == tagRF_BSP.Cur_state)
        {

            //上升沿
            if((1 == PIN_RF_SYNC)
               &&(0 == PIN_Process_Enable))
            {
                //已经收到同步码,并且上次接收的数据已经处理完毕
                if(0 == PIN_First_Valid)
                {
                    PIN_First_Valid   = 1;
                }
                else
                {
                    if ((MIN_ZERO_P_FIVE_BIT_DATA_GAP <= tagRF_BSP.Cur_Low_bits )
                      &&(tagRF_BSP.Cur_Low_bits <=  MAX_ZERO_P_FIVE_BIT_DATA_GAP))
                    {
                        //handle 0.5 bit低电平部分 短
                        if ((MIN_ZERO_P_FIVE_BIT_DATA_GAP <= tagRF_BSP.Back_High_bits )
                            &&(tagRF_BSP.Back_High_bits <=  MAX_ZERO_P_FIVE_BIT_DATA_GAP))
                        {
                             //高电平部分 短
                            if(0 == Ciphertext_Count)
                            {
                                RF_Recevie_Unit(0);
                            }
                            else if(1 == Ciphertext_Back_value)
                            {
                                RF_Recevie_Unit(1);
                            }
                            else
                            {
                                RF_Recevie_Unit(0);
                            }
                        }
                        else  if ((MIN_ONE_BIT_DATA_GAP <= tagRF_BSP.Back_High_bits )
                                &&(tagRF_BSP.Back_High_bits <=  MAX_ONE_BIT_DATA_GAP))
                        {
                             //高电平部分 长
                             RF_Recevie_Unit(0);
                        }
                        else
                        {
                            //高电平部分不符合规定
                            RF_Stop_Rece_Code(RF_ABNORMAL_RECV);
                        }
                    }
                    else if ((MIN_ONE_BIT_DATA_GAP <= tagRF_BSP.Cur_Low_bits )
                                &&(tagRF_BSP.Cur_Low_bits <=  MAX_ONE_BIT_DATA_GAP))
                    {
                         //handle 1 bit  低电平部分 长
                         RF_Recevie_Unit(0);
                         RF_Recevie_Unit(1);
                    }
                    else
                    {
                        // 低电平部分不符合规定
                        RF_Stop_Rece_Code(RF_ABNORMAL_RECV);
                    }
                }
            }

            tagRF_BSP.Back_Low_bits  = tagRF_BSP.Cur_Low_bits;
            tagRF_BSP.Cur_Low_bits   = 0;

        }
        else
        {

            //下降源
           tagRF_BSP.Back_High_bits  = tagRF_BSP.Cur_High_bits;
           tagRF_BSP.Cur_High_bits   = 0;
        }
        tagRF_BSP.Back_state  = tagRF_BSP.Cur_state ;
    }

		#if 0
		//直接把上层解析放在这里
		if (PIN_Process_Enable == 1)
		{
			if (Hitag2(Ciphertext_Buffer+1) == 1)
			{
				memcpy(car_key, Ciphertext_Buffer+1, 12);
			}
			
			//接收密文处理完毕
			PIN_Process_Enable  = 0;
			PIN_RF_SYNC         = 0;
			
			return 1;
		}
		#endif
		
		return 0;
}



/********************************************
* Function Name: RF_Recevie_Unit
* Description  : 接收到一个单元
* Input        : NONE
* Output       : NONE
* Return Value : NONE
********************************************/
void RF_Recevie_Unit(uint8_t unit)
{
    uint8_t  bytes1 = 0;
    uint8_t  bits1  = 0;

    
    
     #if debug_433_def
    if(0 == PIN_Process_Enable)
    {
      bytes1 = 0xAA;
      bytes1 |= unit;
      bitDataTx[bitDataRxIndx++] = bytes1;
      if (bitDataRxIndx >= defBufLen)
      {
        bitDataRxIndx = 0;
      }
    }
      #endif

    bytes1  = Ciphertext_Count>>3;
    bits1   = 7-(Ciphertext_Count&0x07);
    if(unit)
    {
        SET_BIT(Ciphertext_Buffer[bytes1], bits1);
    }
    else
    {
        CLR_BIT(Ciphertext_Buffer[bytes1], bits1);
    }

    Ciphertext_Back_value = unit;
    Ciphertext_Count     ++;

    if(RF_MAX_LENGHT <= Ciphertext_Count)
    {
        RF_Stop_Rece_Code(RF_NORMAL_RECV);
    }
}


/********************************************
* Function Name: RF_Stop_Rece_Code
* Description  : 停止接收加密码
* Input        : 0 : 正常停止
                 1 : 异常停止
* Output       : NONE
* Return Value : NONE
********************************************/
void RF_Stop_Rece_Code(uint8_t  Type)
{
    if(RF_NORMAL_RECV == Type)
    {
        //正常停止接收
        PIN_Process_Enable  = 1;
    }
    else
    {
        //异常停止接收
        PIN_RF_SYNC         = 0;
    }
}



